﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using publicLibrary;

namespace asMeishi.web.Controllers
{
    using Models;

    public class registerController : Controller
    {
        DataSession<MongoDataContext, user> db = new DataSession<MongoDataContext, user>();

        public registerController()
        {

            ViewBag.email = "登录名,不能重复";
            ViewBag.password = "(6~18位)";
            ViewBag.passwordconfirm = "";
            ViewBag.vcode = "";
        }

        //
        // GET: /register/

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 注册提交
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection fc)
        {
            var entity = userC.createDefault();
            TryUpdateModel(entity);

            bool mark = true;

            if (!_emailCheck(entity.email))
            {
                ViewBag.email = "email格式不对，或已经存在";
                mark = false;
            }

            if (fc["password"] != fc["passwordconfirm"])
            {
                ViewBag.passwordconfirm = "2次输入的密码不一致";
                mark = false;
            }

            if (!Validator.IsNormalChar(entity.password))
            {
                ViewBag.password = "密码只能是正常字符 字母，数字，下划线的组合";
                mark = false;
            }

            if (!Validator.IsLengthStr(entity.password, 6, 18))
            {
                ViewBag.password = "密码应为6~18个字符";
                mark = false;
            }

            if (fc["vcode"] != Session["vcode"] as string)
            {
                ViewBag.vcode = "验证码错误";
                mark = false;
            }

            if (mark)
            {
                entity.password = entity.password.md5();
                db.Save(entity);
                userC.login(entity);
                return RedirectToAction("regEnd");
            }

            return View(entity);
        }

        /// <summary>
        /// 注册成功
        /// </summary>
        /// <returns></returns>
        public ActionResult regEnd()
        {
            var entity = userC.loginUser;
            return View(entity);
        }
        

        /// <summary>
        /// email格式检测
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool _emailCheck(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) return false;
            if (!Validator.IsEmail(value)) return false;
            if (db.Where(d => d.email == value).Any()) return false;
            return true;
        }

        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult vCode()
        {
            imgVerifyCode imgVerifyCode = new imgVerifyCode();
            var img = imgVerifyCode.GetByValidateCode().ToArray();
            Session["vcode"] = imgVerifyCode.VCODE;
            return base.File(img, @"image/gif");
        }

        /// <summary>
        /// ajax的email检测
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult emailCheck(string value)
        {
            return Content(_emailCheck(value) ? "ok" : "error");
        }


    }
}
